package com.dataseek.iot.device.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dataseek.iot.device.dto.LiquidJarDto;
import com.dataseek.iot.device.entity.LiquidJar;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface LiquidJarMapper extends BaseMapper<LiquidJar> {

    @Select("<script>" +
            "select\n" +
            "        lj.jar_name,\n" +
            "        lj.jar_code,\n" +
            "        lj.jar_height,\n" +
            "        dl.device_name,\n" +
            "        dl.device_code,\n" +
            "        dl.status,\n" +
            "        dl.alarm_status,\n" +
            "        dl.update_time,\n" +
            "        dl.device_range,\n" +
            "        dl.repair_msg,\n" +
            "        wl.line_name,\n" +
            "        wl.line_code,\n" +
            "        fa.factory_name,\n" +
            "        fa.factory_code,\n" +
            "        lj.jar_leader userName,\n" +
            "        (select dv.att_value from device_att_value dv where dv.device_code=lj.device_code and dv.att_code='liquidLevel') curLevel\n" +
            "        from liquid_jar lj, device_list dl, sys_work_line wl, sys_factory fa\n" +
            "        where 1=1\n" +
            "        and lj.device_code = dl.device_code\n" +
            "        and lj.line_code = wl.line_code\n" +
            "        and wl.factory_code = fa.factory_code" +
            "       <if test=\"LiquidJarDto.jarName!=null and LiquidJarDto.jarName != ''\">" +
            "            and lj.jar_name like '%'||#{LiquidJarDto.jarName}||'%'\n" +
            "        </if>" +
            "        <if test=\"LiquidJarDto.factoryName!=null and LiquidJarDto.factoryName != ''\">" +
            "            and fa.factory_name like '%'||#{LiquidJarDto.factoryName}||'%'\n" +
            "        </if>" +
            "        <if test=\"LiquidJarDto.lineName!=null and LiquidJarDto.lineName != ''\">" +
            "            and wl.line_name like '%'||#{LiquidJarDto.lineName}||'%'\n" +
            "        </if>" +
            "        <if test=\"LiquidJarDto.jarCode!=null and LiquidJarDto.jarCode != ''\">" +
            "            and lj.jar_code like '%'||#{LiquidJarDto.jarCode}||'%'\n" +
            "        </if>" +
            "        <if test=\"LiquidJarDto.deviceCode!=null and LiquidJarDto.deviceCode != ''\">" +
            "            and dl.device_code like '%'||#{LiquidJarDto.deviceCode}||'%'\n" +
            "        </if>" +
            "        <if test=\"LiquidJarDto.status!=null and LiquidJarDto.status != ''\">" +
            "            and dl.status = #{LiquidJarDto.status}\n" +
            "        </if>" +
            "        <if test=\"LiquidJarDto.alarmStatus!=null and LiquidJarDto.alarmStatus ==0\">" +
            "            and cast(dl.alarm_status as varchar) = #{LiquidJarDto.alarmStatus}\n" +
            "        </if>" +
            "        <if test=\"LiquidJarDto.alarmStatus!=null and LiquidJarDto.alarmStatus ==1\">" +
            "            and cast(dl.alarm_status as varchar) in ('1','2','3','4')\n" +
            "        </if>" +
            "</script>"
            )
    List<LiquidJarDto> queryJarShowList(Page<LiquidJarDto> page, @Param("LiquidJarDto") LiquidJarDto qryDto);


    @Select("<script>" +
            "select\n" +
            "        lj.jar_name,\n" +
            "        lj.jar_code,\n" +
            "        lj.jar_height,\n" +
            "        dl.device_name,\n" +
            "        dl.device_code,\n" +
            "        dl.status,\n" +
            "        dl.alarm_status,\n" +
            "        dl.update_time,\n" +
            "        dl.device_range,\n" +
            "        dl.repair_msg,\n" +
            "        wl.line_name,\n" +
            "        wl.line_code,\n" +
            "        fa.factory_name,\n" +
            "        fa.factory_code,\n" +
            "        lj.jar_leader userName,\n" +
            "        (select dv.att_value from device_att_value dv where dv.device_code=lj.device_code and dv.att_code='liquidLevel') curLevel\n" +
            "        from liquid_jar lj, device_list dl, sys_work_line wl, sys_factory fa\n" +
            "        where 1=1\n" +
            "        and lj.device_code = dl.device_code\n" +
            "        and lj.line_code = wl.line_code\n" +
            "        and wl.factory_code = fa.factory_code" +
            "        <if test=\"LiquidJarDto.factoryName!=null and LiquidJarDto.factoryName != ''\">" +
            "            and (fa.factory_name like '%'||#{LiquidJarDto.factoryName}||'%'\n" +
            "            or wl.line_name like '%'||#{LiquidJarDto.factoryName}||'%'\n" +
            "            or lj.jar_code like '%'||#{LiquidJarDto.factoryName}||'%'\n" +
            "            or lj.device_code like '%'||#{LiquidJarDto.factoryName}||'%')\n" +
            "        </if>" +
            "</script>"
    )
    List<LiquidJarDto> queryJarShowIndex(Page<LiquidJarDto> page, @Param("LiquidJarDto") LiquidJarDto qryDto);
}
